<div
    data-service="users.get"
    data-name="user"
    data-event="load,users.update"
    data-param-user-id="{{router.params.id}}"
    data-success="trigger"
    data-success-param-trigger-events="users.get">

    <div class="cover">
        <h1 class="zone xl margin-bottom-large">
            <a data-ls-attrs="href=/console/users?project={{router.params.project}}" class="back text-size-small link-return-animation--start"><i class="icon-left-open"></i> Users</a>
            <br />

            <span data-ls-bind="{{user.name}}">&nbsp;</span>
            <span data-ls-if="{{user.name|escape}} === '' && {{user.email}} !== ''">Unknown</span>
            <span data-ls-if="{{user.name|escape}} === '' && {{user.email}} === ''">Anonymous User</span>
        </h1>
    </div>

    <div data-ui-modal class="modal width-medium box close" data-button-hide="on" data-open-event="open-update-name">
        <button type="button" class="close pull-end" data-ui-modal-close=""><i class="icon-cancel"></i></button>    
        
        <h2>Update name</h2>

        <form name="users.updateName"
            data-analytics
            data-analytics-activity
            data-analytics-event="submit"
            data-analytics-category="console"
            data-analytics-label="Update User Name"
            data-service="users.updateName"
            data-scope="sdk"
            data-event="submit"
            data-success="trigger,alert"
            data-success-param-alert-text="User name was updated successfully"
            data-success-param-trigger-events="users.update"
            data-failure="alert"
            data-failure-param-alert-text="Failed to update user name"
            data-failure-param-alert-classname="error">
            
            <input type="hidden" disabled name="userId" data-ls-bind="{{user.$id}}">

            <label for="name">Name</label>
            <input name="name" id="name" type="text" autocomplete="off" data-ls-bind="{{user.name}}" required class="full-width" maxlength="128">

            <hr />

            <footer>
                <button type="submit" class="">Update</button> &nbsp; <button data-ui-modal-close="" type="button" class="reverse">Cancel</button>
            </footer>
        </form>
    </div>
    
    <div data-ui-modal class="modal width-medium box close" data-button-hide="on" data-open-event="open-update-email">
        <button type="button" class="close pull-end" data-ui-modal-close=""><i class="icon-cancel"></i></button>    
        
        <h2>Update Email</h2>

        <form name="users.updateEmail"
            data-analytics
            data-analytics-activity
            data-analytics-event="submit"
            data-analytics-category="console"
            data-analytics-label="Update User Email"
            data-service="users.updateEmail"
            data-scope="sdk"
            data-event="submit"
            data-success="trigger,alert"
            data-success-param-alert-text="User email was updated successfully"
            data-success-param-trigger-events="users.update"
            data-failure="alert"
            data-failure-param-alert-text="Failed to update user email"
            data-failure-param-alert-classname="error">
            
            <input type="hidden" disabled name="userId" data-ls-bind="{{user.$id}}">

            <label for="email">Email</label>
            <input name="email" id="email" type="text" autocomplete="off" data-ls-bind="{{user.email}}" required class="full-width" maxlength="128">

            <hr />

            <footer>
                <button type="submit" class="">Update</button> &nbsp; <button data-ui-modal-close="" type="button" class="reverse">Cancel</button>
            </footer>
        </form>
    </div>

    <div data-ui-modal class="modal width-medium box close" data-button-hide="on" data-open-event="open-update-password">
        <button type="button" class="close pull-end" data-ui-modal-close=""><i class="icon-cancel"></i></button>    
        
        <h2>Update Password</h2>

        <form name="users.updatePassword"
            data-analytics
            data-analytics-activity
            data-analytics-event="submit"
            data-analytics-category="console"
            data-analytics-label="Update User Password"
            data-service="users.updatePassword"
            data-scope="sdk"
            data-event="submit"
            data-success="trigger,alert"
            data-success-param-alert-text="User password was updated successfully"
            data-success-param-trigger-events="users.update"
            data-failure="alert"
            data-failure-param-alert-text="Failed to update user password"
            data-failure-param-alert-classname="error">
            
            <input type="hidden" disabled name="userId" data-ls-bind="{{user.$id}}">

            <label for="password">Password</label>
            <input name="password" id="password" type="password" autocomplete="off" required class="full-width" maxlength="128">

            <hr />

            <footer>
                <button type="submit" class="">Update</button> &nbsp; <button data-ui-modal-close="" type="button" class="reverse">Cancel</button>
            </footer>
        </form>
    </div>

    <div data-ui-modal class="modal width-large box close" data-button-hide="on" data-open-event="open-json">
        <button type="button" class="close pull-end" data-ui-modal-close=""><i class="icon-cancel"></i></button>    
        
        <h2>JSON View</h2>

        <div class="margin-bottom">
            <input type="hidden" data-ls-bind="{{user}}" data-forms-code />
        </div>

        <button data-ui-modal-close="" type="button" class="reverse">Cancel</button>
    </div>

    <div class="zone xl">
        <ul class="phases clear" data-ui-phases data-selected="{{router.params.tab}}">
            <li data-state="/console/users/user?id={{router.params.id}}&project={{router.params.project}}">
                <h2>Overview</h2>

                <div data-ls-if="{{user.status}} === false" style="display: none" class="box padding-small danger margin-bottom-xxl text-align-center">
                    This user account is blocked.
                </div>

                <div class="row responsive margin-top-xl">
                    <div class="col span-8">
                        <label>&nbsp;</label>

                        <div class="box margin-bottom-large">
                            <div class="text-align-center">
                                <img src="" data-ls-attrs="src={{user|avatar}}" data-size="200" alt="User Avatar" class="avatar huge margin-top-negative-xxl" />

                                <div class="margin-top-small" data-ls-bind="Member since {{user.registration|dateText}}"></div>
                                <div class="margin-top-small">
                                    <span data-ls-if="{{user.emailVerification}} === true">
                                        <span class="tag green">Verified</span>
                                    </span>
                                    <span data-ls-if="{{user.emailVerification}} !== true">
                                        <span class="tag">Unverified</span>
                                    </span>
                                </div>
                                <div class="margin-top-small" data-ls-bind="{{user.email}}"></div>
                            </div>
                        </div>

                        <h3 class="margin-bottom">Preferences</h3>

                        <div class="box margin-bottom"
                            data-service="users.getPrefs"
                            data-name="user-prefs"
                            data-event="load"
                            data-param-user-id="{{router.params.id}}"
                            data-success="trigger"
                            data-success-param-trigger-events="users.getPrefs">

                            <div data-ls-if="!{{user-prefs|isEmptyObject}}">
                                <table class="vertical">
                                    <thead>
                                        <tr>
                                            <th>Key</th>
                                            <th>Value</th>
                                        </tr>
                                    </thead>
                                    <tbody data-ls-loop="user-prefs" data-ls-as="pref">
                                        <tr>
                                            <td data-title="Key: ">
                                                <span data-ls-bind="{{$index}}"></span>
                                            </td>
                                            <td data-title="Value: ">
                                                <span data-ls-bind="{{pref}}"></span>
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>

                            <div data-ls-if="{{user-prefs|isEmptyObject}}">
                                No user preferences found
                            </div>
                        </div>

                        <h3 class="text-danger">Danger Zone</h3>

                        <div class="box danger margin-bottom">
                            <p>This is the area where you can delete this user.</p>

                            <p>By deleting this user you will lose all data associated with this user.</p>

                            <p>PLEASE NOTE: User deletion is irreversible.</p>

                            <form class="inline"
                                data-analytics
                                data-analytics-activity
                                data-analytics-event="submit"
                                data-analytics-category="console"
                                data-analytics-label="Delete User"
                                data-service="users.delete"
                                data-event="submit"
                                data-param-user-id="{{router.params.id}}"
                                data-success="alert,trigger,redirect"
                                data-confirm="Are you sure you want to delete this user?"
                                data-success-param-alert-text="Deleted user successfully"
                                data-success-param-trigger-events="users.delete"
                                data-success-param-redirect-url="/console/users?project={{router.params.project}}"
                                data-failure="alert"
                                data-failure-param-alert-text="Failed to delete user"
                                data-failure-param-alert-classname="error">
                                <input type="hidden" name="projectId" data-ls-bind="{{router.params.project}}" />

                                <button class="danger reverse" type="submit">Delete User</button>
                            </form>
                        </div>
                    </div>
                    <div class="col span-4 sticky-top">
                        <label>User ID</label>
                        <div class="input-copy margin-bottom">
                            <input id="uid" type="text" autocomplete="off" placeholder="" data-ls-bind="{{user.$id}}" disabled data-forms-copy>
                        </div>

                        <ul class="margin-bottom-large text-fade text-size-small">
                            <li class="margin-bottom-small"><i class="icon-angle-circled-right margin-start-tiny margin-end-tiny"></i> <button data-ls-ui-trigger="open-update-name" class="link text-size-small">Update name</button></li>
                            <li class="margin-bottom-small"><i class="icon-angle-circled-right margin-start-tiny margin-end-tiny"></i> <button data-ls-ui-trigger="open-update-email" class="link text-size-small">Update Email</button></li>
                            <li class="margin-bottom-small"><i class="icon-angle-circled-right margin-start-tiny margin-end-tiny"></i> <button data-ls-ui-trigger="open-update-password" class="link text-size-small">Update Password</button></li>
                            <li class="margin-bottom-small"><i class="icon-angle-circled-right margin-start-tiny margin-end-tiny"></i> <button data-ls-ui-trigger="open-json" class="link text-size-small">View as JSON</button></li>
                        </ul>

                        <div data-ls-if="{{user.status}} === true" style="display: none">
                            <form name="users.updateStatus" class="margin-bottom"
                                data-analytics
                                data-analytics-activity
                                data-analytics-event="submit"
                                data-analytics-category="console"
                                data-analytics-label="Update User Status"
                                data-service="users.updateStatus"
                                data-event="submit"
                                data-param-user-id="{{router.params.id}}"
                                data-success="alert,trigger"
                                data-success-param-alert-text="Blocked user successfully"
                                data-success-param-trigger-events="users.update"
                                data-failure="alert"
                                data-failure-param-alert-text="Failed to block user"
                                data-failure-param-alert-classname="error">

                                <button name="status" type="submit" class="danger fill" value="false" data-cast-to="boolean">Block Account</button>
                            </form>
                        </div>

                        <div data-ls-if="{{user.status}} === false" style="display: none">
                            <form name="users.updateStatus" class="margin-bottom"
                                data-analytics
                                data-analytics-activity
                                data-analytics-event="submit"
                                data-analytics-category="console"
                                data-analytics-label="Update User Status"
                                data-service="users.updateStatus"
                                data-event="submit"
                                data-param-user-id="{{router.params.id}}"
                                data-success="alert,trigger"
                                data-success-param-alert-text="Activated user successfully"
                                data-success-param-trigger-events="users.update"
                                data-failure="alert"
                                data-failure-param-alert-text="Failed to activate user"
                                data-failure-param-alert-classname="error">

                                <button name="status" type="submit" class="fill" value="true" data-cast-to="boolean">Activate Account</button>
                            </form>
                        </div>
                    </div>
                </div>
            </li>
            <li data-state="/console/users/user/sessions?id={{router.params.id}}&project={{router.params.project}}">
                <h2>Sessions</h2>

                <div
                    data-service="users.getSessions"
                    data-name="sessions"
                    data-param-user-id="{{router.params.id}}"
                    data-event="load,users.update">

                    <div data-ls-if="{{sessions.sessions.length}} === 0" style="display: none" class="margin-top-xxl margin-bottom-xxl text-align-center">
                        No sessions available.
                    </div>

                    <div data-ls-if="{{sessions.sessions.length}} !== 0" style="display: none">
                        <div class="box margin-bottom">
                            <ul data-ls-loop="sessions.sessions" data-ls-as="session" class="list">
                                <li class="clear">
                                    <form class="pull-end"
                                        data-analytics
                                        data-analytics-activity
                                        data-analytics-event="submit"
                                        data-analytics-category="console"
                                        data-analytics-label="Delete User Session"
                                        data-service="users.deleteSession"
                                        data-event="submit"
                                        data-loading="Loading..."
                                        data-success="trigger"
                                        data-success-param-trigger-events="users.update"
                                        data-failure="alert"
                                        data-failure-param-alert-text="Failed to logout session"
                                        data-failure-param-alert-classname="error">

                                        <input type="hidden" name="userId" data-ls-bind="{{router.params.id}}">
                                        <input type="hidden" name="sessionId" data-ls-bind="{{session.$id}}">
                                        <button class="danger">Logout</button>
                                    </form>
                                    <div class="pull-start margin-end avatar-container">
                                        <img onerror="this.onerror=null;this.className='avatar hide'" data-ls-if="{{session.clientCode|lowercase}} !== 'cli'" data-ls-attrs="src={{env.API}}/avatars/browsers/{{session.clientCode|lowercase}}?width=120&height=120&project={{env.PROJECT}},title={{session.clientName}},alt={{session.clientName}}" class="avatar" loading="lazy" width="60" height="60" />

                                        <img onerror="this.onerror=null;this.className='avatar hide'" data-ls-if="{{session.clientCode|lowercase}} === 'cli'" data-ls-attrs="src=/images/clients/terminal.png?buster=<?php echo APP_CACHE_BUSTER; ?>,title={{session.clientName}},alt={{session.clientName}}" class="avatar" loading="lazy" width="60" height="60" />
                                        
                                        <div data-ls-if="{{session.provider}} !== 'email'" class="corner">
                                            <img data-ls-attrs="src=/images/users/{{session.provider}}.png?buster=<?php echo APP_CACHE_BUSTER; ?>,title={{session.provider}},alt={{session.provider}}"  class="avatar xs" loading="lazy" width="30" height="30" />
                                        </div>
                                    </div>

                                    <span data-ls-bind="{{session.clientName}}"></span> <span data-ls-bind="{{session.clientVersion}}"></span> on <span data-ls-bind="{{session.deviceModel}}"></span> <span data-ls-bind="{{session.osName}}"></span> <span data-ls-bind="{{session.osVersion}}"></span>

                                    <div class="margin-top-small">
                                        <img onerror="this.onerror=null;this.className='avatar hide'" data-ls-if="{{session.countryCode}} !== '--'" data-ls-attrs="src={{env.API}}/avatars/flags/{{session.countryCode}}?width=120&height=120&project={{env.PROJECT}}" class="avatar xxs margin-end-small inline" />
                                        <small data-ls-bind="{{session.ip}}"></small> / <small data-ls-bind="{{session.countryName}}"></small>
                                    </div>
                                </li>
                            </ul>
                        </div>

                        <form class="inline margin-bottom-large"
                            data-analytics
                            data-analytics-activity
                            data-analytics-event="submit"
                            data-analytics-category="console"
                            data-analytics-label="Delete User Sessions"
                            data-service="users.deleteSessions"
                            data-param-user-id="{{router.params.id}}"
                            data-event="submit"
                            data-success="trigger"
                            data-success-param-trigger-events="users.update"
                            data-failure="alert"
                            data-failure-param-alert-text="Failed to logout all sessions"
                            data-failure-param-alert-classname="error">

                            <button class="danger">Logout from all sessions</button>
                        </form>
                    </div>
                </div>

            </li>
            <li data-state="/console/users/user/audit?id={{router.params.id}}&project={{router.params.project}}">
                <h2>Activity</h2>

                <div
                    data-service="users.getLogs"
                    data-name="logs"
                    data-param-user-id="{{router.params.id}}"
                    data-event="load,logs-load">

                    <div data-ls-if="{{logs.logs.length}} === 0" style="display: none" class="margin-top-xxl margin-bottom-xxl text-align-center">
                        No logs available.
                    </div>

                    <div class="box" data-ls-if="{{logs.logs.length}} !== 0" style="display: none">
                        <table class="vertical small">
                            <thead>
                                <tr>
                                    <th width="140">Date</th>
                                    <th width="175">Event</th>
                                    <th>Client</th>
                                    <th width="90">Location</th>
                                    <th width="90">IP</th>
                                </tr>
                            </thead>
                            <tbody data-ls-loop="logs.logs" data-ls-as="log">
                                <tr>
                                    <td data-title="Date: "><span data-ls-bind="{{log.time|dateTime}}"></span></td>
                                    <td data-title="Event: "><span data-ls-bind="{{log.event}}"></span></td>
                                    <td data-title="Client: ">
                                        <img onerror="this.onerror=null;this.className='avatar hide'" data-ls-if="{{log.clientCode|lowercase}} !== 'cli'" data-ls-attrs="src={{env.API}}/avatars/browsers/{{log.clientCode|lowercase}}?width=80&height=80&project={{env.PROJECT}},title={{log.clientName}},alt={{log.clientName}}" class="avatar xxs inline margin-end-small" />
                                        
                                        <img onerror="this.onerror=null;this.className='avatar hide'" data-ls-if="{{log.clientCode|lowercase}} === 'cli'" data-ls-attrs="src=/images/clients/terminal.png?buster=<?php echo APP_CACHE_BUSTER; ?>,,title={{log.clientName}},alt={{log.clientName}}" class="avatar xxs inline margin-end-small" />
                                        
                                        <span data-ls-if="({{log.clientName}})" data-ls-bind="{{log.clientName}} {{log.clientVersion}} on {{log.model}} {{log.osName}} {{log.osVersion}}"></span>
                                        <div data-ls-if="(!{{log.clientName}})" class="text-align-center text-fade">Unknown</div>
                                    </td>
                                    <td data-title="Location: ">
                                        <img onerror="this.onerror=null;this.className='avatar hide'" data-ls-if="{{log.countryCode}} !== '--'" data-ls-attrs="src={{env.API}}/avatars/flags/{{log.countryCode}}?width=80&height=80&project={{env.PROJECT}}" class="avatar xxs inline margin-end-small" />
                                        <span data-ls-bind="{{log.countryName}}"></span>
                                    </td>
                                    <td data-title="IP: "><span data-ls-bind="{{log.ip}}"></span></td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </li>
        </ul>
    </div>
</div>
